/**
 * Created by user on 2015/9/1.
 */


angular.module('backofficeApp.menuManager.controllers', [])
    .controller('menuCtrl', ['$scope','$http', '$location', function ($scope, $http, $location) {
        $scope.currentPage = 1;
        $scope.pageSize = 15;

        //菜单分页
        $scope.pagination = new pagination({currentPage:$scope.currentPage,pageSize: $scope.pageSize,
            url:'http://' + backOfficeHost + '/rest/v1.0/backoffice/menu/menuList/:page/:size',
            ajax: $http,
            success:function(data){
                $scope.menuList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.currentPage = $(".am-active").text();
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $('#count').text("共"+$scope.pageNumber+"页 有"+$scope.count+"数据");

            },
            error: function(err){
                $scope.menuList = null;
            },
            params: {}
        });


        $scope.search = function() {

            $scope.pagination.currentPage = 1;
            $scope.currentPage = 1;

            var body = {};
            if ($scope.desc) body.menuName = $scope.desc;

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/menu/menuList/' + 1 + '/' + $scope.pageSize, body, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                $scope.menuList = data.data.list;
                $scope.pageNumber = data.data.totalPage;
                $scope.count = data.data.count;
                $scope.pagination.totalPage = data.data.totalPage;
                $scope.pagination.pageHeader($scope.pageNumber);
                $scope.pagination.searchParams = body;
            }).error(function () {
                $scope.menuList = null;
            });
        };


        //全选列表
        $scope.allSelected = function(){
            $scope.pagination.allSelected();
        };
        //删除
        $scope.delete = function(){
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/menu/menuDelete',
                checkObj: $(".item-check"),//checkbox对象
                key: "menuID",
                success: function(data){
                    var number = $(".am-active").text();//当前页码
                    $scope.pagination.selectPage(number);
                    alert("删除成功！");
                    $scope.menuUpdate(data);
                },
                error: function(){}
            };
            $scope.pagination.listDelete(params);
        };
        //新增
        $scope.addData = {};
        $scope.add = function(addData){
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/menu/menuAdd',
                addData: addData,
                success: function(data){
                    $scope.pagination.selectPage(1);//插入成功显示第一页
                    $('#doc-modal-add').modal("close");
                    if(data.result.status == 1){
                        alert("新增成功！");
                        $scope.addData = {};
                        $scope.menuUpdate(data);
                    }else{
                        alert("新增失败！");
                    }
                },
                error: function(){
                    $('#doc-modal-add').modal("close");
                    alert("新增失败！");
                }
            };
            $scope.pagination.listAdd(params);
        };
        //编辑
        $scope.update = function(updateData,index){
            var uData = {menuID:"",menu:{}};
            if(updateData.sys_menuID)uData.menuID = updateData.sys_menuID;
            if(updateData.menuType != null)uData.menu.menuType = updateData.menuType;else {alert("请输入菜单类型");return;}
            if(updateData.menuName)uData.menu.menuName = updateData.menuName;
            if(updateData.parentID != null)uData.menu.parentID = updateData.parentID;
            if(updateData.permissionsCategoryID)uData.menu.permissionsCategoryID = updateData.permissionsCategoryID;
            if(updateData.menuOrder != null)uData.menu.menuOrder = updateData.menuOrder;
            if(updateData.pagePath)uData.menu.pagePath = updateData.pagePath;
            if(updateData.imagePath)uData.menu.imagePath = updateData.imagePath;
            if(updateData.menuClass)uData.menu.menuClass = updateData.menuClass;
            if(updateData.isValid)uData.menu.isValid = Number(updateData.isValid);
            if(updateData.remark)uData.menu.remark = updateData.remark;
            //uData = deleteObjNull(uData);
            var params = {
                url: 'http://' + backOfficeHost + '/rest/v1.0/backoffice/menu/menuEdit',
                updateData: uData,
                success: function(data){
                    var pageNumber = $(".am-active").text();//当前页码
                    $('#doc-modal-'+index).modal("close");
                    //$scope.pagination.selectPage(pageNumber);
                    if(data.result.status == 1){
                        alert("修改成功！");
                        $scope.menuUpdate(data);
                    }else{
                        alert("修改失败！");
                    }
                },
                error: function(){
                    //var number = $(".am-active").text();//当前页码
                    $('#doc-modal-'+index).modal("close");
                    alert("修改失败！");
                }
            };
            $scope.pagination.listUpdate(params);
        };

        //更新菜单
        $scope.menuUpdate = function(data){
            gObj.leftObj.paint(data.data);
            var sdata = JSON.stringify(data.data);
            sessionStorage.setItem("leftMenuData",sdata);
        }

    }]);